package com.wanlian.router.ui.integral;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.TextView;

import com.smart.android.ui.tools.BaseListFragment;
import com.smart.android.ui.web.WebActivity;
import com.wanlian.router.R;
import com.wanlian.router.app.AppConfig;
import com.wanlian.router.bean.Integral;
import com.wanlian.router.bean.IntegralFlow;
import com.wanlian.router.bean.IntegralTotal;
import com.wanlian.router.net.NetWorkingClient;
import com.wanlian.router.net.ResponseData;
import com.wanlian.router.net.internal.INetCallBack;
import com.wanlian.router.storage.AppData;
import com.wanlian.router.ui.integral.intc.TransferINTActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Hyu on 2018/9/4.
 * Email: fvaryu@163.com
 */
public class IntIntegralFragment extends BaseListFragment {

    private IncomeFlowAdapter mIncomeFlowAdapter;
    private List<IntegralFlow> mIntegralFlows;
    @BindView(R.id.tv_total)
    TextView mTotal;

    @BindView(R.id.tv_rule)
    TextView tv_rule;

    public static IntIntegralFragment newInstance() {

        Bundle args = new Bundle();
        IntIntegralFragment fragment = new IntIntegralFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int layout() {
        return R.layout.fragment_int_integral;
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        tv_rule.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        mIntegralFlows = new ArrayList<>();

        setAdapter(mIncomeFlowAdapter = new IncomeFlowAdapter(getActivity(), mIntegralFlows));
    }

    @Override
    protected void initData(View view) {
        super.initData(view);

        NetWorkingClient.getInstance().queryIntegral(getActivity(),0, new INetCallBack<List<IntegralTotal> >() {
            @Override
            public void onFinish(ResponseData response, @Nullable List<IntegralTotal>  integralTotals) {
                if (response.isSuccess()) {

                    if (integralTotals != null && integralTotals.size() > 0) {
                        IntegralTotal integralTotal = integralTotals.get(0);
                        String t = String.format(Locale.getDefault(),"%.6f ≈ %s元", integralTotal.getScore(), integralTotal.getRmb());
                        int index = t.indexOf(".");
                        SpannableString ss = new SpannableString(t);
                        ss.setSpan(new RelativeSizeSpan(0.5f), index, t.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
                        mTotal.setText(ss);
                    }
                }else {
                    showToast(response.getMessage());
                }
            }
        });



    }

    @Override
    protected void loadData(final boolean isRefresh) {
        super.loadData(isRefresh);
        if (!canToLoad(isRefresh)) {
            return;
        }
        NetWorkingClient.getInstance().queryIntegralFlow(getActivity(), getPageInfo(), new INetCallBack<List<IntegralFlow>>() {
            @Override
            public void onFinish(ResponseData response, @Nullable List<IntegralFlow> integralFlows) {
                resetPull();
                if (response.isSuccess()) {
                    if (isRefresh) {
                        mIntegralFlows.clear();
                    }
                    if (integralFlows != null) {
                        mIntegralFlows.addAll(integralFlows);
                    }

                    mIncomeFlowAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @OnClick(R.id.tv_transfer)
    void transfer() {
        redirectActivity(TransferINTActivity.class);
    }

    @OnClick(R.id.tv_rank)
    void rank() {
        redirectActivity(IntegralRankActivity.class);
    }

    @OnClick(R.id.tv_telecom)
    void telecom() {
        redirectActivity(IntegralMallActivity.class);
    }

    @OnClick(R.id.tv_title)
    void title() {
        new AlertDialog.Builder(getActivity())
                .setMessage("12312312313")
                .setPositiveButton("知道了", null)
                .create().show();
    }

    @OnClick(R.id.tv_rule)
    void rule() {
        WebActivity.fire(getActivity(), AppData.DEFAULT.get(AppConfig.SCORE_ROLE));
    }
}
